/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2008 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#ifndef INCL_EVENT_DATASTORE
#define INCL_EVENT_DATASTORE

#include "PIMDataStore.h"
#include "SQLiteDriver.h"
#include "Date.h"
#include "Event.h"
#include "AlarmDataStore.h"
#include "RecurrenceDataStore.h"
#include "constants.h"
#include "base/Log.h"

using namespace Funambol;

class EventDataStore : PIMDataStore {

private:
    /**
    * The SQLiteDriver to store in the database
    */
    SQLiteDriver driver;
    
    /**
    * Get a StringBuffer in the format needed starting from an Date object
    */
    StringBuffer getDate(Date& d);

    /**
    * Get a StringBuffer in the format needed starting from an Timezone object
    */
    StringBuffer getTimezoneName(Timezone& tz);
    
    /**
    * Get a StringBuffer in the format needed starting from an bool object
    */
    StringBuffer getBooleanValue(bool val);
    
    /**
    * Creates the query to insert the item in the storage
    */
    StringBuffer createInsertEventStmt(Event& item);
    StringBuffer createInsertEventChangesStmt(Event& event, const char* identifier);
    StringBuffer createInsertCalendarChangesStmt(Event& event);
    StringBuffer createInsertOccurrenceCacheStmt(Event& event, const char* identifier);
    
    /**
     * Creates the query to delete the item from the storage
     */
    StringBuffer createDeleteEventStmt(const char* id);
    StringBuffer createDeleteEventChangesStmt(Event& event, const char* id);
    StringBuffer createDeleteOccurrenceCacheStmt(const char* id);        
        
    /**
     * Creates the query to update an item in the storage
     */    
    StringBuffer createUpdateEventStmt(const char* identifier, Event& event);
    StringBuffer createUpdateEventChangesStmt(const char* identifier, Event& event);
    StringBuffer createUpdateSingleOccurrenceCacheStmt(Event& event, const char* identifier);
    
    /**
     * It handles the Alarm object of the event if exist. 
     * The action means which is the action that is done on the main event
     * 0 means the event is adding
     * 1 means the event is updating
     * 2 means the event is deleting
     */
    int handleAlarm(Event& event, const char* identifier, int action);
    
    /**
    * Updates the table to take account of the modification from the client
    */    
    StringBuffer createUpdateFunEventStateStmt(const char* id);
    
    /**
    * It populates an Event object from an arrayList with all the fields populated from the
    * db.
    * @param al the arraylist containing all the fields
    * @return the Event object filled with the data. If there is any error, the returned value is NULL
    */
    Event* populateEvent(const ArrayList& al);
  
    /**
     * It is an instance of the RecurrenceDataStore used only to store the Recurrence part of the event
     * if it exists.
     */
    AlarmDataStore alarmds;

    /**
     * It is an instance of the RecurrenceDataStore used only to store the Recurrence part of the event
     * if it exists. @todo to be implemented
     */
     RecurrenceDataStore recurrenceds;
    
    /**
     * Select the calendar is where the event must be inserted. Currently the policy is get the maximum
     * id of the table Calendar. It can be reviewed
     */
    void setValidCalendarId(Event& event);
    
    /**
     * Get the current calendar_id from the event stored in the db. The choice is to get it from the 
     * existing event stored in the db because if in the future we exchanged it in vcal, the only thing is
     * remove the getPreviousCalendarId and setExistingCalendarId mothods
     * 
     */
    void getPreviousCalendarId(Event& event, const char* identifier);
    
public:
    
    EventDataStore(const char* cpath) : driver(cpath), alarmds(driver), recurrenceds(driver) { }
        
    ~EventDataStore() {}

        
    int addPIMItem(StringBuffer& identifier, const PIMItem& item);

    int updatePIMItem(const char* identifier, const PIMItem& item);

    int deletePIMItem(const char* identifier);

    PIMItem* getPIMItem(const char* identifier);
    
   /**
    * It checks for the table that are in the db of calendar. It they are not there, 
    * the method initialize them
    */
    int checkDBFunTable();
    
    int resetFlags(unsigned long lastsync);
    
    /**
     * Used for the deletion of all items. It takes advantage of the native trigger
     * that are in the calendar db. The trigger deletes all the other tables at cascade...
     * it also delete the fun_event_state table that doesn't need anymore (due to a slow sync...)
     */
    int removeAllItems();
    
};
#endif
